import {
  accountLoginRequest,
  requestUserMenusByRoleId
} from '@/service/login/login'
import { IAccount } from '@/service/login/type'
import { Module } from 'vuex'
import { IRootState } from '../../type'
import { ISystemState } from './type'
import LocalCache from '@/utils/cache'
import { getPageListData, editPageData } from '@/service/main/system/system'
import cache from '@/utils/cache'
import dayjs from 'dayjs'

const system: Module<ISystemState, IRootState> = {
  namespaced: true, //命名空间
  state: {
    memberList: '',
    membertotal: '',
    leaveList: '',
    leavetotal: '',
    newuserList: '',
    newusertotal: '',
    makeList: '',
    maketotal: '',
    evaluateList: '',
    evaluatetotal: '',
    newsList: '',
    newstotal: ''
  },
  mutations: {
    changeMemberList(state, memberList: any[]) {
      state.memberList = memberList
    },
    changeMemberCount(state, membertotal: number) {
      state.membertotal = membertotal
    },
    changeLeaveList(state, leaveList: any[]) {
      state.leaveList = leaveList
    },
    changeLeaveCount(state, leavetotal: number) {
      state.leavetotal = leavetotal
    },
    changeNewuserList(state, newuserList: any[]) {
      state.newuserList = newuserList
    },
    changeNewuserCount(state, newusertotal: number) {
      state.newusertotal = newusertotal
    },
    changeMakeList(state, makeList: any[]) {
      state.makeList = makeList
    },
    changeMakeCount(state, maketotal: number) {
      state.maketotal = maketotal
    },
    changeEvaluateList(state, evaluate: any[]) {
      state.evaluateList = evaluate
    },
    changeEvaluateCount(state, evaluate: number) {
      state.evaluatetotal = evaluate
    },
    changeNewsList(state, evaluate: any[]) {
      state.newsList = evaluate
    },
    changeNewsCount(state, evaluate: number) {
      state.newstotal = evaluate
    }
  },
  getters: {
    //拿数据中的List数据 根据名字的不同拿不一样的数据
    pageListData(state) {
      return (pageName: string) => {
        if (pageName == 'leave/list') {
          return (state as any)[`leaveList`]
        } else if (pageName == 'newuser/list') {
          return (state as any)[`newuserList`]
        } else if (pageName == 'make/list') {
          return (state as any)[`makeList`]
        } else if (pageName == 'Getnews') {
          return (state as any)[`newsList`]
        } else {
          return (state as any)[`${pageName}List`]
        }
      }
    },
    //拿数据中的Count数据 根据名字的不同拿不一样的数据
    pageListCount(state) {
      return (pageName: string) => {
        if (pageName == 'leave/list') {
          return (state as any)[`leavetotal`]
        } else if (pageName == 'newuser/list') {
          return (state as any)[`newusertotal`]
        } else if (pageName == 'make/list') {
          return (state as any)[`maketotal`]
        } else if (pageName == 'Getnews') {
          return (state as any)[`newstotal`]
        } else {
          return (state as any)[`${pageName}total`]
        }
      }
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      console.log('调用system里面的网络请求')
      // 1.获取pageUrl
      const pageName = payload.pageName
      const pageUrl = `/${pageName}`

      // 2.对页面发送请求
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)

      // 3.将数据存储到state中
      const { list, total } = pageResult.data
      if (pageName == 'leave/list') {
        commit('changeLeaveList', list)
        commit('changeLeaveCount', total)
      } else if (pageName == 'newuser/list') {
        commit('changeNewuserList', list)
        commit('changeNewuserCount', total)
      } else if (pageName == 'make/list') {
        commit('changeMakeList', list)
        commit('changeMakeCount', total)
      } else if (pageName == 'Getnews') {
        commit('changeNewsList', list)
        commit('changeNewsCount', total)
      } else {
        const changePageName =
          pageName.slice(0, 1).toUpperCase() + pageName.slice(1)

        commit(`change${changePageName}List`, list)
        commit(`change${changePageName}Count`, total)
      }
    },
    //编辑数据
    async editPageDataAction({ dispatch }, payload: any) {
      // 1.编辑数据的请求
      let { editData, id } = payload
      console.log(payload)

      let { pageName } = payload
      editData['id'] = id
      console.log(editData)
      const pageUrl = `/${pageName}`
      await editPageData(pageUrl, editData)

      if (pageName === 'user') {
        pageName = 'member'
      }
      // 2.请求最新的数据
      if (pageName == 'leave/check') {
        pageName = 'leave/list'
        dispatch('getPageListAction', {
          pageName,
          queryInfo: {
            pageNum: 1,
            pageSize: 10,
            type: 0
          }
        })
      } else if (pageName == 'newuser/runcheck') {
        pageName = 'newuser/list'
        dispatch('getPageListAction', {
          pageName,
          queryInfo: {
            pageNum: 1,
            pageSize: 10,
            type: 0
          }
        })
      } else if (pageName == 'make/list') {
        let time = ''
        if (cache.getCache('SelectvalueTime') != null) {
          time = cache.getCache('SelectvalueTime')
        } else {
          time = dayjs().format('YYYY-MM-DD hh:mm:ss')
        }
        dispatch('getPageListAction', {
          pageName,
          queryInfo: {
            pageNum: 1,
            pageSize: 10,
            type: 0,
            classify: payload.editData.classify,
            thatVeryDay: time
          }
        })
      } else if (pageName == 'evaluate') {
        pageName = 'evaluate'
        dispatch('getPageListAction', {
          pageName,
          queryInfo: {
            pageNum: 1,
            pageSize: 10,
            type: 0
          }
        })
      } else if (pageName == 'Getnews') {
        pageName = 'evaluate'
        dispatch('getPageListAction', {
          pageName,
          queryInfo: {
            pageNum: 1,
            pageSize: 10,
            type: 0
          }
        })
      } else {
        dispatch('getPageListAction', {
          pageName,
          queryInfo: {
            pageNum: 1,
            pageSize: 10
          }
        })
      }
    }
  }
}

export default system
